package com.blackandwhitesensation.sice.filter;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.blackandwhitesensation.sice.modelo.Usuario;
import com.blackandwhitesensation.sice.modelo.UsuarioSistema;



public class LoginFilter implements Filter {
	
	private List<String> administrador = new ArrayList<String>();
	private List<String> root= new ArrayList<String>();
	private List<String> empleado= new ArrayList<String>();
	private String webContext;
	private boolean flagEntry;

	public void destroy() {
		// TODO Auto-generated method stub
	}
	
	
	public void init(FilterConfig filterConfig) throws ServletException {
		webContext=filterConfig.getServletContext().getContextPath();
		administrador = llenarAdministrador();
		empleado = llenarEmpleado();
		llenarRoot();
		
	}
	
	
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		
		String path = httpServletRequest.getContextPath();
		String basePath = httpServletRequest.getScheme()+"://"+httpServletRequest.getServerName()+":"+httpServletRequest.getServerPort()+path+"/";
			
		HttpSession session = httpServletRequest.getSession(false);
		String pageRequested = httpServletRequest.getRequestURI().toString();
		

		if (session == null ) {
			session = httpServletRequest.getSession(true);
			httpServletResponse.sendRedirect(basePath);
		}
		
		
		UsuarioSistema usuario = (UsuarioSistema) session.getAttribute("usuarioSistema");
		
		
		
		if(usuario != null && !pageRequested.contains("inicio.xhtml")){
			
			if (usuario.getTipoUsuarioSistema().getIdTipoUsuarioSistema() == 3 && empleado.contains(pageRequested)) {
				flagEntry=true;
				chain.doFilter(request, response);
			}
			
			if(usuario.getTipoUsuarioSistema().getIdTipoUsuarioSistema() == 2 && administrador.contains(pageRequested)){
				flagEntry=true;
				chain.doFilter(request, response);
			}
			
			if(usuario.getTipoUsuarioSistema().getIdTipoUsuarioSistema() == 1 &&
			  usuario.getUsuario().equals("EUREKA") && usuario.getContraseA().equals("apps") &&
			  root.contains(pageRequested)){
				flagEntry=true;
				chain.doFilter(request, response);
			}
			
			if(flagEntry==false){
				session = httpServletRequest.getSession(true);
				session.setAttribute("usuarioSistema", null);
				//session.invalidate();
				httpServletResponse.sendRedirect(basePath);
			}
			
			
		}
		
		else{
			session = httpServletRequest.getSession(true);
			session.setAttribute("usuarioSistema", null);
			//session.invalidate();
			httpServletResponse.sendRedirect(basePath);
			
			
		}
		
			flagEntry=false;	
	}

	
	
	
	public List<String> llenarAdministrador(){
		
		administrador.add(webContext+"/XHTML/usuarioSistema.xhtml");
		administrador.add(webContext+"/XHTML/tipoUsuarioSistema.xhtml");
		administrador.add(webContext+"/XHTML/menuAdministrador.xhtml");
		administrador.add(webContext+"/XHTML/tipoMembresia.xhtml");
		
		return administrador;
		
	}
	
	public List<String> llenarEmpleado(){
		
		empleado.add(webContext+"/XHTML/initialMenu.xhtml");
		empleado.add(webContext+"/XHTML/usuario.xhtml");
		empleado.add(webContext+"/XHTML/ingresarEvento.xhtml");
		
		return empleado;
		
	}
	
	
	public void llenarRoot(){
		root.add(webContext+"/XHTML/rootMenu.xhtml");
		root.add(webContext+"/XHTML/evento.xhtml");
		root.add(webContext+"/XHTML/eventoListDataTable.xhtml");
		
	}

}
